﻿using System;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using Plumber.Accounting.Forms;
using Plumber.Accounting.ModuleData;
using Plumber.Masters.ModuleData;
using ApplicationLibrary;

namespace Plumber.Accounting.Library
{
    internal class JournalEntriesSalePurchaseCreator : IDocumentDataManager, IDisposable
    {
        private JournalEntriesSalePurchase je = null;
        private DRJournalEntryForSales DRJournalForSales = null;
        private DRJournalEntryForCreditNote DRJournalForCreditNote = null;
        private DRMasterTypeByType DRMasterTypeByType = null;

        private InvoiceType invoiceType;

        public JournalEntriesSalePurchaseCreator(InvoiceType invType)
        {
            je = new JournalEntriesSalePurchase();
            je.ShowDialog(false);
            je.SilentMode = true;
            invoiceType = invType; 

            DRJournalForSales = new DRJournalEntryForSales();
            DRJournalForCreditNote = new DRJournalEntryForCreditNote();
            DRMasterTypeByType = new DRMasterTypeByType();
        }

        public bool AddNew()
        {
            return je.ToolbarEvent(DocumentForm.ToolbarEventKind.New);
        }

        public bool Edit()
        {
            return je.ToolbarEvent(DocumentForm.ToolbarEventKind.Edit);
        }

        public bool Save()
        {
            return je.ToolbarEvent(DocumentForm.ToolbarEventKind.Save);
        }

        public bool Delete()
        {
            return je.ToolbarEvent(DocumentForm.ToolbarEventKind.Delete);
        }

        public bool Find(IRadarParameters key)
        {
            return je.FindRecord(key);
        }

        public bool Find(string journalNo)
        {
            RadarJournalEntryParam param = new RadarJournalEntryParam(journalNo);
            return Find(param);
        }

        public void SetHeader(string template, string documentNo, DateTime docDate, string mastertype, string code, string description, InvoiceType invType, string invoiceNoRef)
        {
            je.GetTemplate.SelectedValue = template;
            je.GetDocNo.Text = documentNo;
            je.GetDocDate.Today = docDate;
            je.GetMasterType.SelectedValue = mastertype;
            je.GetCode.Text = code;
            je.GetInvoice.Text = invoiceNoRef;
            je.GetCode.UpdateDescription();
            je.IsEditable.Value = false;
            je.DontUpdateSale = true;
            je.GetDocumentType.SelectedValue = invType.Int();
            invoiceType = invType;
        }

        public void AddRow(string masterType, string code, string description, string document, double from, double to, string invoiceNo)
        {
            je.AddNewRow(masterType, document, code, from, to, description, true, false, invoiceNo);
        }

        public void AddCastleRow(string taxcode, double amount, double tax, bool bankrestraint, bool deduction, bool restraint, string restraintcode, double restraintamount)
        {
            je.AddCastleRow(taxcode, amount, tax, bankrestraint, deduction, restraint, restraintcode, restraintamount);
        }

        public void LoadTemplate()
        {
            je.LoadTemplate();
        }

        public void ClearJE()
        {
            je.GetDataGrid.Clear();
        }

        public void ClearEmptyRow()
        {
            je.ClearEmptyRow();
        }

        public DataGridViewRow GetRow(int t)
        {
            return je.GetDataGrid.Rows[t];
        }

        public void ClearCastle()
        {
            je.GetCastleTax.Clear();
        }

        public DataGridViewRow GetCastleRow(int t)
        {
            return je.GetCastleTax.Rows[t];
        }

        public int RowCount
        {
            get { return je.GetDataGrid.Rows.Count; }
        }

        public int CastleRowCount
        {
            get { return je.GetCastleTax.Rows.Count; }
        }

        public string SearchFromDocumentNo(string type, string code, string document)
        {
            if (invoiceType == InvoiceType.E_CreditNoteEm || invoiceType == InvoiceType.E_DebitNoteRc)
            {
                if (DRJournalForCreditNote.Find(type, code, document))
                    return DRJournalForCreditNote.GetValue<string>(PL_JournalEntryDetail.JournalNo);
            }
            else
            {
                if (DRJournalForSales.Find(type, code, document))
                    return DRJournalForSales.GetValue<string>(PL_JournalEntryDetail.JournalNo);
            }
            return "";
        }

        public string GetMasterTypeByType(MasterTypes type)
        {
            if (DRMasterTypeByType.Find(type))
                return DRMasterTypeByType.GetValue<string>(PL_MasterType.Code);

            return "";
        }

        public void Dispose()
        {
            je.Close();
            je.Dispose();
            je = null;
        }
    }
}